package fr.futsal.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import fr.futsal.dao.EquipeDAOImpl;
import fr.futsal.dao.EquipeDAO;
import fr.futsal.domaine.Equipe;

/**
 * Servlet implementation class Equipe
 */
@WebServlet("/Equipes.html")
public class EquipeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@Resource(name="jdbc/futsal")
	private DataSource futsalDS;

	private List<Equipe> getListeEquipe() {

		Connection connection = null;
		try {
			connection = futsalDS.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		
		try{
			EquipeDAO dao = new EquipeDAOImpl(connection);
			
			return dao.collecteEquipe();

		}finally{
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		List<Equipe> listeEquipe = getListeEquipe();
		
		request.setAttribute("listeEquipe", listeEquipe);
		
		request.getRequestDispatcher("/Equipe.jsp").forward(request, response);
		
	}
}
